package com.woniuxy.yoga.product.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.statedata.ProductState;
import com.woniuxy.yoga.product.dao.model.ProductType;
import com.woniuxy.yoga.product.dao.mapper.ProductTypeMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.yoga.product.model.dto.NormalProductTypeDTO;
import com.woniuxy.yoga.product.model.dto.ProductTypeDTO;
import com.woniuxy.yoga.product.model.exception.ProductException;
import com.woniuxy.yoga.product.model.exception.ProductExceptionCode;
import com.woniuxy.yoga.product.model.param.AddProductTypeParam;
import com.woniuxy.yoga.product.model.param.QueryProductTypeParam;
import com.woniuxy.yoga.product.service.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品类型表 服务实现类
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年12月01日
 */
@Service
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements ProductTypeService {

    @Autowired
    private ProductTypeMapper productTypeMapper;

    /**
     * 新增产品类型
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addProductType(AddProductTypeParam param) {
        QueryWrapper<ProductType> wrapper = new QueryWrapper<>();
        wrapper.eq("product_type_name",param.getProductTypeName());
        wrapper.notIn("product_type_state", ProductState.PRODUCT_TYPE_STATE_DELETE);
        //判断是否已经存在该类型
        ProductType productType = productTypeMapper.selectOne(wrapper);
        if (productType!=null) throw new ProductException(ProductExceptionCode.PRODUCT_EXCEPTION_CODE_TYPE_EXIST);
        //对象转换
        ProductType type = BeanUtil.toBean(param, ProductType.class);
        //设置为禁用状态 确认无误之后再确认启用
        type.setProductTypeState(ProductState.PRODUCT_TYPE_STATE_DISABLE);
        productTypeMapper.insert(type);
    }

    /**
     * 上架产品类型
     * @param productTypeId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modifyProductType(Integer productTypeId) {
        ProductType productType = productTypeMapper.selectById(productTypeId);
        //判断是否存在该产品类型
        if (productType==null) throw new ProductException(ProductExceptionCode.PRODUCT_EXCEPTION_CODE_TYPE_NOT_EXIST);
        //判断产品状态是否异常
        if (productType.getProductTypeState()!=ProductState.PRODUCT_TYPE_STATE_DISABLE) throw new ProductException(ProductExceptionCode.PRODUCT_EXCEPTION_CODE_TYPE_STATE_ERROR);
        //修改状态
        productType.setProductTypeState(ProductState.PRODUCT_TYPE_STATE_NORMAL);
        productTypeMapper.updateById(productType);
    }

    /**
     * 删除产品类型
     * @param productTypeId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteProductType(Integer productTypeId) {
        ProductType productType = productTypeMapper.selectById(productTypeId);
        //判断是否存在该产品类型
        if (productType==null) throw new ProductException(ProductExceptionCode.PRODUCT_EXCEPTION_CODE_TYPE_NOT_EXIST);
        //判断产品状态是否异常
        if (productType.getProductTypeState()!=ProductState.PRODUCT_TYPE_STATE_DISABLE) throw new ProductException(ProductExceptionCode.PRODUCT_EXCEPTION_CODE_TYPE_STATE_ERROR);
        //修改状态
        productType.setProductTypeState(ProductState.PRODUCT_TYPE_STATE_DELETE);
        productTypeMapper.updateById(productType);
    }

    /**
     * 查询所有产品类型
     * @param param
     * @return
     */
    @Override
    public PageInfo queryProductType(QueryProductTypeParam param) {
        QueryWrapper<ProductType> wrapper = new QueryWrapper<>();
        //索引排序查询
        wrapper.orderByAsc("product_type_id");
        //分页查询
        Page<ProductType> page = new Page<>(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 5 : param.getPageSize());
        productTypeMapper.selectPage(page,wrapper);
        //数据转换
        List<ProductTypeDTO> productTypeDTOS = new ArrayList<>();
        if (page.getRecords()!=null&&page.getRecords().size()>0){
            for (ProductType productType : page.getRecords()) {
                //对象转换
                ProductTypeDTO productTypeDTO = BeanUtil.toBean(productType, ProductTypeDTO.class);
                //状态赋值
                String productTypeState = productType.getProductTypeState()==0?"已上架":productType.getProductTypeState()==1?"已下架":"已删除";
                productTypeDTO.setProductTypeState(productTypeState);
                //数据保存到集合
                productTypeDTOS.add(productTypeDTO);
            }
        }
        //分页对象转换
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setRecords(productTypeDTOS);
        return pageInfo;
    }

    /**
     * 商家查询上架产品类型
     * @return
     */
    @Override
    public List<NormalProductTypeDTO> queryNormalProductTypeDTO() {
        QueryWrapper<ProductType> wrapper = new QueryWrapper<>();
        //上架状态
        wrapper.eq("product_type_state",ProductState.PRODUCT_TYPE_STATE_NORMAL);
        //索引排序
        wrapper.orderByAsc("product_type_id");
        //查询数据
        List<ProductType> productTypes = productTypeMapper.selectList(wrapper);
        //集合转换
        return BeanUtil.copyToList(productTypes,NormalProductTypeDTO.class);
    }

    @Override
    public List<NormalProductTypeDTO> userQueryNormalProductTypeDTO() {
        return this.queryNormalProductTypeDTO();
    }
}
